package cn.tedu.system.merchant.service.impl;

import cn.tedu.system.base.exception.ServiceException;
import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.base.response.StatusCode;
import cn.tedu.system.base.utils.CacheUtil;
import cn.tedu.system.merchant.mapper.MerchantMapper;

import cn.tedu.system.merchant.pojo.dto.*;
import cn.tedu.system.merchant.pojo.entity.Merchant;

import cn.tedu.system.merchant.pojo.vo.MerchantVO;

import cn.tedu.system.merchant.service.MerchantService;
import cn.tedu.system.product.mapper.ProductMapper;
import cn.tedu.system.product.pojo.dto.ProductDto;
import cn.tedu.system.product.pojo.dto.ProductPutDto;
import cn.tedu.system.product.pojo.dto.ProductReviseDto;
import cn.tedu.system.product.pojo.entity.Product;
import cn.tedu.system.product.pojo.vo.ProductVO;
import cn.tedu.system.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CacheUtil cacheUtil;

    //添加店铺
    @Override
    public void addStore(MerchantDto merchantDto, HttpSession session) {
        log.debug("添加店铺业务: {}",merchantDto);

        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantDto,merchant);
        merchant.setUserId(userVO.getId());
        merchant.setStatus(0);
        merchant.setCreateTime(new Date());
        merchant.setUpdateTime(new Date());

        if(merchantMapper.addStore(merchant)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //删除店铺
    @Override
    public void delStore(StoreDeleteDTO storeDeleteDTO, HttpSession session) {
        log.debug("删除店铺业务: {}", storeDeleteDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        if( merchantMapper.delStore(storeDeleteDTO)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //修改店铺信息
    @Override
    public void reviseStore(MerchantReviseDto merchantReviseDto, HttpSession session) {
        log.debug("merchantReviseDto: {}",merchantReviseDto);

        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantReviseDto,merchant);
        merchant.setUserId(userVO.getId());
        merchant.setUpdateTime(new Date());

        if(merchantMapper.reviseStore(merchant)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //查看店铺
    @Override
    public List<MerchantVO> selStore(HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }
        List<MerchantVO> merchantVO = merchantMapper.selStore(userVO.getId());
        if(merchantVO.size()==0 && merchantVO==null){
            throw new ServiceException(StatusCode.FAILED);
        }
        return merchantVO;
    }

    //添加商品
    @Override
    public void addProduct(ProductDto productDto, HttpSession session) {
        log.debug("添加商品业务: {}",productDto);

        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        //如果店铺审核还没有通过就不允许添加商品
        Long merchantId = productDto.getMerchantId();
        int status = merchantMapper.selectStatus(merchantId);
        if(status==0){
            throw new ServiceException("该店铺的审核还没有通过，请耐心等待！");
        }
        Product product = new Product();
        //BeanUtils.copyProperties(productDto,product);
        product.setMerchantId(productDto.getMerchantId());
        product.setName(productDto.getName());
        product.setDescription(productDto.getDescription());
        product.setPrice(productDto.getPrice());
        product.setStock(productDto.getStock());
        product.setUnit(productDto.getUnit());
        product.setImageUrls(productDto.getImageUrls());
        product.setOrigin(productDto.getOrigin());

        product.setSales(34);
        product.setStatus(0);
        product.setIsAgriculture("0");
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());

        if(productMapper.addProduct(product)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //删除商品
    @Override
    public void delProduct(ProductDeleteDTO deleteDTO, HttpSession session) {
        log.debug("删除商品业务: {}", deleteDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        if (productMapper.delProduct(deleteDTO) <= 0) { // 传递DTO对象
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //修改商品信息
    @Override
    public void reviseProduct(ProductReviseDto productDto, HttpSession session) {
        log.debug("修改商品业务: {}",productDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Product product = new Product();
        BeanUtils.copyProperties(productDto,product);
        product.setUpdateTime(new Date());

        if(productMapper.reviseProduct(product)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }

    //查看所有商品功能
    @Override
    public List<ProductVO> selAllProduct(ProductPutDto productPutDto, HttpSession session) {
        List<ProductVO> productVOList = productMapper.selAllProduct(productPutDto);
        if (productVOList == null || productVOList.isEmpty()) {
            throw new ServiceException(StatusCode.FAILED);
        }
        return productVOList;
    }

    //商品上下架
    @Override
    public void isPutProduct(ProductPutDto productPutDto, HttpSession session) {
        log.debug("商品上下架业务: {}",productPutDto);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            throw new ServiceException(StatusCode.UNAUTHORIZED);
        }

        Product product = new Product();
        BeanUtils.copyProperties(productPutDto,product);
        product.setUpdateTime(new Date());

        if(productMapper.isPutProduct(product)<=0){
            throw new ServiceException(StatusCode.FAILED);
        }
    }


}
